import path from "path";
import vuePlugin from "rollup-plugin-vue";
import commonjs from "rollup-plugin-commonjs";
import typescript from "rollup-plugin-typescript2";
import babel from "@rollup/plugin-babel";
import resolve from "@rollup/plugin-node-resolve";
import json from "@rollup/plugin-json";
import postcss from "rollup-plugin-postcss";
import cssnano from "cssnano";
import autoprefixer from "autoprefixer";
import svg from "rollup-plugin-svg";
import image from "@rollup/plugin-image";
import PluginAlias from "@rollup/plugin-alias";
import copy from "rollup-plugin-copy";
import clear from "rollup-plugin-clear";
import { loadJsonFileSync } from "load-json-file";
let { name } = loadJsonFileSync("./plugin_config/plugin_base_config.json");
let { buildPath } = loadJsonFileSync("./plugin_config/plugin_build_entry.json");
let pluginName = name;
const customResolver = resolve({
  extensions: [".mjs", ".vue", ".js", ".jsx", ".json", ".sass", ".scss"],
});
const entry = () => path.resolve(buildPath);
const basePlugins = () => [
  vuePlugin(),
  clear({
    targets: ["./dist"],
  }),
  PluginAlias({
    entries: [
      { find: "@", replacement: path.resolve("./src") },
      {
        find: "@com",
        replacement: path.resolve("./src/components"),
      },
      {
        find: "@pages",
        replacement: path.resolve("./src/pages"),
      },
    ],
    customResolver,
  }),
  typescript(),
  commonjs(),
  postcss({
    plugins: [autoprefixer(), cssnano()],
    extensions: ["css", "scss", "less", "styl"],
  }),
  babel({
    babelHelpers: "bundled",
    exclude: "node_modules/**",
  }),
  copy({
    targets: [
      {
        src: "./plugin_config/plugin_base_config.json",
        dest: `./dist/${pluginName}`,
        rename: "config.json",
      },
    ],
  }),
  resolve({ browser: true }),
  json(),
  image(),
  svg(),
];
const external = ["vue", "vue-i18n", "vuex", "vue-router"];
const genConfig = (name) => ({
  input: entry(),
  output: {
    file: `dist/${name}/${name}.js`,
    format: "iife", //打包生成格式
    name: "", //设置为空 直接解析
    globals: {
      // 设定全局变量的名称 window.Vue=Vue VueI18n Vuex VueRouter
      vue: "Vue",
      "vue-i18n": "VueI18n",
      vuex: "Vuex",
      "vue-router": "VueRouter",
    },
    exports: "named",
  },
  plugins: basePlugins(),
  external,
});
export default [genConfig(pluginName)];
